function flip_page(elm_id, base_url)
{
	var elm = $('#'+elm_id);
	if (elm != null)
	{
		var target = base_url + elm.val();
		window.location = target;
	}
}

function checkbox_switch(elm_id)
{
	var elm = $('#'+elm_id);
	if (elm != null)
		if (elm.attr("checked"))
			$("#"+elm_id+"_division").hide();
    	else
    		$("#"+elm_id+"_division").show();
}

function geography_autocomplete(elm_id, json_url, cur_code, cur_lang, allow_empty)
{
	$.ajax({
		type:"GET",
		url:json_url,
		cache:true,
		dataType:"json",

		success:function(data){
			if (allow_empty) {$('#'+elm_id).append('<option value=""></option>')};
			$.each(data, function(i) {
				var alias = '';
				var local_name = '';
				var selected = '';
				$.each(data[i].localNames.locales, function(key, val) {
					alias = alias + ' ' + val;
					if (key == cur_lang) {local_name = val;}
				});
				if (data[i].code == cur_code) {selected = ' selected';}
  				 var row="<option value=\""+data[i].code+"\" data-alternative-spellings=\""+alias+"\" "+selected+">"+local_name+' ' +data[i].code+"</option>";
  				 $('#'+elm_id).append(row);
			});
			$('#'+elm_id).selectToAutocomplete();
		}
	});
}

function tagit_init(elm_id, json_url)
{
	$.ajax({
		type:"GET",
		url:json_url,
		cache:false,
		dataType:"json",
		success:function(data){
			$('#'+elm_id).tagit({
				availableTags: data,
				itemName: elm_id,
		        allowSpaces: true
		    });
		}
	});
}

function tag_autocomplete(elm_id, json_url, type, cur_code, cur_lang, allow_empty)
{
	$.ajax({
		type:"GET",
		url:json_url,
		data: {"type":type},
		cache:false,
		dataType:"json",

		success:function(data){
			if (allow_empty) {$('#'+elm_id).append('<option value="">Search</option>')};
			$.each(data, function(i) {
				var alias = '';
				var local_name = '';
				var selected = '';
				$.each(data[i].name, function(key, val) {
					alias = alias + ' ' + val;
					if (key == cur_lang) {local_name = val;}
				});
				if (data[i].string1 == cur_code) {selected = ' selected';}
  				 var row="<option value=\""+data[i].string1+"\" data-alternative-spellings=\""+alias+"\" "+selected+">"+local_name+' ' +data[i].string1+"</option>";
  				 $('#'+elm_id).append(row);
			});
			$('#'+elm_id).selectToAutocomplete();
		}
	});
}